#version 130

in vec3 a_Vertex;
in vec3 a_Normal;
in vec3 a_Tangent;
in vec3 a_Binormal;
in vec2 a_Texcoord0;
in vec4 a_Color; //The color attribute which was passed in from the program
out vec4 color; //The output color which will be passed to the fragment shader
out vec3 B;
out vec3 T;
out vec3 N;
out vec2 texcoord0;

void main(void)
{
	vec3 n_Vertex = a_Vertex;
	vec3 n_Normal = a_Normal;
	vec3 n_Tangent = a_Tangent;
	vec3 n_Binormal = -a_Binormal;
	
	n_Vertex.z=-n_Vertex.z;
	n_Normal.z=-n_Normal.z;
	n_Tangent.z=-n_Tangent.z;
	n_Binormal.z=-n_Binormal.z;
	
	//vec4 light0_position = vec4(0.0,1.0,0.2,0.0);
	//Transform the normal into eye space using the normal matrix
	T = n_Tangent;
	//T.x=-T.x;
	B = n_Binormal;
	N = n_Normal;
	//N.x=-N.x;
	//B = normalize(a_Tangent.w * cross(N,T));
	T = normalize(gl_NormalMatrix * T);
	B = normalize(gl_NormalMatrix * B);
	N = normalize(gl_NormalMatrix * N);
	//N = normalize(gl_NormalMatrix * N);
	/*//Calculate the light direction, in a directional light the
	//position is actually a direction vector
	//We transform the position into eyespace before normalizing the vector
	vec3 L = normalize(gl_ModelViewMatrix *light0_position).xyz;
	//We calculate the angle between the normal and the light direction
	float NdotL = max(dot(N, L), 0.0);
	//The ambient color is fixed, so we add this as the initial color and
	//then build on this with the other lighting contributions
	vec4 finalColor = vec4(0.2);
	//Do the standard vertex transformation into eye space
	vec4 pos = gl_ModelViewMatrix * vec4(a_Vertex, 1.0);
	//Because we are in eye space (everything is relative to the camera)
	//The eye vector is simply the negated position.
	vec3 E = -pos.xyz;
	//If the surface normal is facing towards the light at all
	if (NdotL > 0.0)
	{
		//Add the diffuse color using Lambertian Reflection
		finalColor += NdotL;
		//Calculate the half vector and make it unit length
		vec3 HV = normalize(L + E);
		//Find the angle between the normal and the half vector
		float NdotHV = max(dot(N, HV), 0.0);
		//Calculate the specular using Blinn-Phong
		float glossRaw = 0.2f;
		float gloss = pow(2.0f,13.0f*glossRaw);
		finalColor += pow(NdotHV,(gloss))*(2.0+gloss)/8.0;
	}*/

	gl_Position = gl_ModelViewProjectionMatrix * vec4(n_Vertex, 1.0);
	//color.rgb = normalize(B);
	color=a_Color;
	texcoord0 = vec2(a_Texcoord0.x,1.0-a_Texcoord0.y);
}